const app = getApp();
const { AskPage } = app._require('common/index');
const formMixin = app._require("mixins/form.mixin.js").default;
const share = app._require("pages/task-share-form/handleShare").default;
AskPage({
    /**
     * 页面的初始数据
     */
    name: "taskFillList",
    mixins: [formMixin],
    data: {
        tableData: [],
        readonly: false,
        pageSize: 20,
        tempPageNumber: 1,
        currentPage: 1,
        total: 0,
        taskId: "",
        searchValue: "",
        sortValue: "",
        sortList: [
            { label: "正序排序", value: "1" },
            { label: "倒序排序", value: "2" },
        ],
        sortTypeValue: "",
        sortTypeList: [
            { label: "发布时间", value: "create" },
            { label: "截至日期", value: "end" },
            { label: "任务名称", value: "name" },
        ],
        taskStateValue: "",
        taskStateList: [
            { label: "发布时间", value: "create" },
            { label: "截至日期", value: "end" },
            { label: "任务名称", value: "name" },
            { label: "截至日期1", value: "end1" },
            { label: "任务名称2", value: "name2" },
        ],
        endTime: "",
        createTime: "",
    },
    onLoad: function (options) {
        let { taskId, readonly } = options;
        this.setData({
            taskId: taskId,
            readonly: readonly === "1",
        });
    },
    onShow() {
        this.initData(this.data.tempPageNumber || 1, true);
    },
    handleDropClick(e) {
        let { key } = e.currentTarget.dataset;
        let { value: item } = e.detail;
        // this.selectComponent("#sortDrop").toggle();
        this.setData({
            [key]: item.value || "",
        });
    },
    handleTypeDropClick(e) {
        let { key } = e.currentTarget.dataset;
        let { value: item } = e.detail;
        // this.selectComponent("#typeDrop").toggle();
        this.setData({
            [key]: item.value || "",
        });
    },
    onDateSure(e) {
        let { key } = e.currentTarget.dataset;
        let { value } = e.detail;
        // this.selectComponent("#typeDrop").toggle();
        this.setData({
            [key]: value || "",
        });
    },
    onChange() {
        let { key } = e.currentTarget.dataset;

        this.setData({
            [key]: item.value || "",
        });
    },
    onSearch() {},
    onReachBottom() {
        let page = this.data.currentPage;
        this.initData(page);
    },
    onPullDownRefresh() {
        this.setData({
            tempPageNumber: 1
        });
        this.initData().finally(() => {
            wx.stopPullDownRefresh();
        });
    },
    onReachBottom() {
        let page = this.data.currentPage;
        this.initData(page + 1).finally(() => {
            console.log("oh");
        });
    },
    getInitParams() {
        return {
            url: "taskFill/list",
            data: {
                taskId: this.data.taskId || "",
            },
        };
    },
    handleClick(e) {
        let { value: item } = e.detail;
        console.log(e);
        this.setData({
            tempPageNumber: item.page || 1,
        });
    },
    async initData(page = 1, part = false) {
        let { pageSize } = this.data;
        let otherParams = this.getInitParams();
        // let { stateValue, filterValue, searchText, addByTime } = this.data.model;
        try {
            const response = await this.$_server(otherParams.url, {
                data: {
                    currentPage: page,
                    pageSize: pageSize,
                    ...otherParams.data,
                },
            });
            let { data } = response;
            if (data.code !== 200) {
                throw data;
            }
            let { data: list } = data;
            const tableData = this.buildTableData(list.records || [], page);
            let _tableData = [];
            if (page === 1) {
                _tableData = tableData;
            } else if (part) {
                let _start = (page-1) * this.data.pageSize;
                this.data.tableData.splice(_start, this.data.pageSize, ...tableData);
                console.log(tableData, this.data.tableData);
                _tableData = this.data.tableData;
            } else {
                _tableData = this.data.tableData.concat(tableData);
            }
            if (list.pages && page > list.pages) {
                page = list.pages;
            }
            this.setData({
                currentPage: part ? this.data.currentPage : page,
                tableData: _tableData,
                total: list.total || 0,
            });
        } catch (error) {
            console.log(error);
            // statements
            if (error.message) {
                wx.showToast({
                    mask: true,
                    title: error.message,
                    icon: "none",
                    duration: 2000,
                });
            } else {
                throw error;
            }
        }
    },
    buildTableData(list,page) {
        let res = [];
        if (list.length === 0) {
            return res;
        }
        res = list.map((cur, index) => {
            return {
                page,
                taskId: this.data.taskId,
                id: cur.id || "",
                cover: "",
                // 任务名称
                // name: cur.name || "",
                name: "数据项" + page+"-"+index,
                index: index,
                // 截至时间
                endTime: cur.finishTime || "",
                createTime: cur.createTime || "",
                source: cur,
            };
        });
        return res;
    },
    add(){
        share.handleForm(
            {
                taskId: this.data.taskId,
                isCms: "0",
                line: this.data.total ? this.data.total + 1 : 1,
            },
            "navigateTo"
        );
    },
    loginSuccess(){
        this.initData();
    }
});
